require(['topBar', 'layout', 'date', 'gaodeMap', 'grid'], function () {
    //提交到后台参数
    var postData,
        $grid = $('#grid'),
        $dialog = $('#dialog'),
        mapObj;//地图对象
    $('#top').topBar({
        left: [{
            type: 'filter-more',
            search: function () {
                if (getPostData()) {
                    mapObj.map.clearMap();
                    $.ajaxExt({
                        url: "/electFencAlm/searchElectFencStatis.htm",
                        data: postData
                    }).done(function (data) {
                        if (data) {
                            $.each(data, function (i, item) {
                                createElectFence(item);
                            });
                        }
                    });
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    hidden:true,
                    type: 'dateLine'
                }
            ]
        }]
    });

    //创建电子围栏
    function createElectFence(item) {

        //获取电子围栏信息
        $.ajaxExt({
            url: "/electFencSet/getBaseElectFencById.htm",
            data: {'fencId': item.fencId}
        }).done(function (data) {
            var electFenceObj;
            if (data.fencType == 1) {
                electFenceObj = createPolygon(data);
            } else {
                electFenceObj = createCircle(data);
            }

            var infoWindow = new AMap.InfoWindow({
                content: '电子围栏名称：{0}<br>次数：{1}'.format(item.fencName, item.cnt)
            });

            AMap.event.addListener(electFenceObj, 'click', function () {
                showDialog(item.fencCd);
            });

            AMap.event.addListener(electFenceObj, 'mouseover', function (e) {
                infoWindow.open(mapObj.map, e.lnglat);
            });

            AMap.event.addListener(electFenceObj, 'mouseout', function () {
                infoWindow.close();
            });
        });
    }

    //画多边形（电子围栏）
    function createPolygon(electFencInfo) {
        var polygonArr = [];
        $.each(electFencInfo.list, function (i, item) {
            polygonArr.push([item.fencLgt, item.fencLtt]);
            if (i == 0) {
                mapObj.setMapCenterAndLevel(item.fencLgt, item.fencLtt, 13);
            }
        });

        var polygon = new AMap.Polygon({
            path: polygonArr,//设置多边形边界路径
            strokeColor: "#FF33FF", //线颜色
            strokeOpacity: 0.2, //线透明度
            strokeWeight: 3,    //线宽
            fillColor: "#1791fc", //填充色
            fillOpacity: 0.35//填充透明度
        });
        polygon.setMap(mapObj.map);
        return polygon;
    }

    //画圈（电子围栏）
    function createCircle(electFencInfo) {
        var circle = new AMap.Circle({
            center: new AMap.LngLat(electFencInfo.coreLgt, electFencInfo.coreLtt),// 圆心位置
            radius: electFencInfo.coreRadius, //半径
            strokeColor: "#F33", //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 3, //线粗细度
            fillColor: "#ee2200", //填充颜色
            fillOpacity: 0.35//填充透明度
        });
        circle.setMap(mapObj.map);
        mapObj.setMapCenterAndLevel(electFencInfo.coreLgt, electFencInfo.coreLtt, 13);
        return circle;
    }

    $('body').layout();

    $('.close-btn', $dialog).on('click', function () {
        $dialog.hide();
    });

    $grid.grid({
        url: "/electFencAlm/searchElectFencAlmPage.htm",
        width: 550,
        height: 440,
        colModel: [
            {name: "licPltNo", label: '车牌号', width: 100},
            {name: "rcrdTime", label: '发生时间', width: 170},
            {name: "inoutType", label: '方向', width: 80}
        ],
        multiselect: false
    });

    function showDialog(fencCd) {
        $dialog.show();
        $('#gridContent').css({
            top: ($(window).height() - 485) / 2,
            left: ($(window).width() - 560) / 2
        });

        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: $.extend({}, postData, {fencCd: fencCd})
        }).trigger("reloadGrid");
    }

    //获取左侧选择的节点信息
    function getPostData() {
        var rcx = commonFun.getTreeCheckNodeToArgs();
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
        }
        return rcx;
    }

    mapObj = new MapGaoDe(107.49023438, 36.24427318, 2);
});